home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / mat < prev    next >
Encoding:
Text File  |  2012-05-30  |  4.7 KB  |  150 lines

  1. #!/usr/bin/python
  2. '''
  3.     Metadata anonymisation toolkit - CLI edition
  4. '''
  5.  
  6. import sys
  7. import xml.sax
  8. import optparse
  9. import os
  10.  
  11. import hachoir_core
  12.  
  13. from lib import mat
  14. from lib import strippers
  15.  
  16.  
  17. def parse():
  18.     '''
  19.         Get, and parse options passed to the program
  20.     '''
  21.     parser = optparse.OptionParser(usage='%prog [options] files\n\
  22. The default behaviour is to clean files given in argument')
  23.     options = optparse.OptionGroup(parser, 'Options')
  24.     options.add_option('--add2archive', '-a', action='store_true',
  25.         default=False, help='Add to output archive non-supported filetypes')
  26.     options.add_option('--backup', '-b', action='store_true', default=False,
  27.         help='Keep a backup copy')
  28.     options.add_option('--force', '-f', action='store_true', default=False,
  29.         help='Don\'t check if files are clean before cleaning')
  30.  
  31.     info = optparse.OptionGroup(parser, 'Informations')
  32.     info.add_option('--check', '-c',  action='store_true', default=False,
  33.         help='Check if a file is free of harmful metadatas')
  34.     info.add_option('--display', '-d', action='store_true', default=False,
  35.         help='List all the harmful metadata of a file without removing them')
  36.     info.add_option('--list', '-l', action='store_true', default=False,
  37.         help='List all supported fileformat')
  38.     info.add_option('--version', '-v', action='callback',
  39.         callback=display_version, help='Display version and exit')
  40.     parser.add_option_group(options)
  41.     parser.add_option_group(info)
  42.  
  43.     values, arguments = parser.parse_args()
  44.     if not arguments and values.list is False:
  45.         # if no argument and no files are passed,
  46.         # print help and exit
  47.         parser.print_help()
  48.         sys.exit(0)
  49.     return values, arguments
  50.  
  51.  
  52. def display_version(*_):
  53.     '''
  54.         Display the program's version, and exit
  55.     '''
  56.     print('Metadata Anonymisation Toolkit version %s') % mat.__version__
  57.     print('Hachoir version %s') % hachoir_core.__version__
  58.     sys.exit(0)
  59.  
  60.  
  61. def list_meta(class_file, filename, force):
  62.     '''
  63.         Print all the metadata of 'filename' on stdout
  64.     '''
  65.     print('[+] File %s :' % filename)
  66.     if force is False and class_file.is_clean():
  67.         print('No harmful metadata found')
  68.     else:
  69.         meta = class_file.get_meta()
  70.         print ('Harmful metadata found:')
  71.         if meta is not None:
  72.             for key, value in class_file.get_meta().iteritems():
  73.                 print('\t' + key + ' : ' + str(value))
  74.  
  75.  
  76. def is_clean(class_file, filename, force):
  77.     '''
  78.         Say if 'filename' is clean or not
  79.     '''
  80.     if class_file.is_clean():
  81.         print('[+] %s is clean' % filename)
  82.     else:
  83.         print('[+] %s is not clean' % filename)
  84.  
  85.  
  86. def clean_meta(class_file, filename, force):
  87.     '''
  88.         Clean the file 'filename'
  89.     '''
  90.     print('[+] Cleaning %s' % filename)
  91.     if force is False and class_file.is_clean():
  92.         print('%s is already clean' % filename)
  93.     else:
  94.         if class_file.remove_all():
  95.             print('%s cleaned !' % filename)
  96.         else:
  97.             print('Unable to clean %s', filename)
  98.  
  99.  
  100. def list_supported():
  101.     '''
  102.         Print all supported fileformat, and exit
  103.     '''
  104.     handler = mat.XMLParser()
  105.     parser = xml.sax.make_parser()
  106.     parser.setContentHandler(handler)
  107.     path = mat.get_sharedir('FORMATS')
  108.     with open(path, 'r') as xmlfile:
  109.         parser.parse(xmlfile)
  110.  
  111.     for item in handler.list:
  112.         if strippers.STRIPPERS.has_key(item['mimetype'].split(',')[0]):
  113.             #on display localy supported formats
  114.             print 'ok'
  115.             print('%s (%s)' % (item['name'], item['extension']))
  116.             print('\tsupport : ' + item['support'])
  117.             print('\tmetadata : ' + item['metadata'])
  118.             print('\tmethod : ' + item['method'] + '\n')
  119.             if item['support'] == 'partial':
  120.                 print('\tremaining : ' + item['remaining'] + '\n')
  121.     sys.exit(0)
  122.  
  123.  
  124. def main():
  125.     '''
  126.         main function : get args, and launch the appropriate function
  127.     '''
  128.     args, filenames = parse()
  129.  
  130.     #func receive the function correponding to the options given as parameters
  131.     if args.display is True:  # only print metadatas
  132.         func = list_meta
  133.     elif args.check is True:  # only check if the file is clean
  134.         func = is_clean
  135.     elif args.list is True:  # print the list of all supported format
  136.         list_supported()
  137.     else:  # clean the file
  138.         func = clean_meta
  139.  
  140.     for filename in filenames:
  141.         class_file = mat.create_class_file(filename, args.backup,
  142.             args.add2archive)
  143.         if class_file is not None:
  144.             func(class_file, filename, args.force)
  145.         else:
  146.             print('Unable to process %s' % filename)
  147.  
  148. if __name__ == '__main__':
  149.     main()
  150.